package com.sgzn.sgpay.service.tcp.model.terminal.request;

import com.sgzn.sgpay.service.tcp.SgTcpProtocolHead;
import com.sgzn.sgpay.service.tcp.util.SgTcpUtil;
import lombok.Data;
import org.hibernate.validator.constraints.NotBlank;

/**
 * TCP上分请求类
 * @author huangsenguang
 * @version Id: TcpIncreaseCreditRequest.java, v 0.1 2018/7/15 8:21 huangsenguang Exp $$
 */
@Data
public class TcpIncreaseCreditRequest extends TcpCommonRequest {

    /**
     * 长度
     */
    private String length="08";

    /**
     * 索引
     */
    private String index="01";

    /**
     * 命令
     */
    private String cmd="03";

    /**
     * Data[0]:自增加码。 需要改成自动累加
     */
    private String increase;

    /**
     * Data[1-2]:投币数。
     */
    private String coinAmount;

    /**
     * Data[3-4]:金额
     */
    @NotBlank
    private  String amount;

    @Override
    public String toString(){
        //根据协议规范重载toString方法
        return beginCode + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                serialCode + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                deviceId + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                commandReq + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                head + length + index + cmd + getData() +
                getAllCheck(length + index + cmd + getData()) + end + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                checkSumCode + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                endCode;
    }

    /**
     * 获取校验码
     * @param needToCheck
     * @return
     */
    private String getAllCheck(String needToCheck) {
        this.setCheck(SgTcpUtil.getToCheck(needToCheck));
        return this.check;
    }

    /**
     * 获取Data 数据
     *   Data[0]:自增加码。
     Data[1-2]:投币数。
     Data[3-4]:金额

     * @return
     */
    private String getData() {
        return SgTcpUtil.cutOrSupplement(increase, 2) + getConvertResult(coinAmount) + getConvertResult(amount);
    }

    /**
     * 将低位在前的字节数组转换成16进制字符串
     * @param sourceString
     * @return
     */
    private String getConvertResult(String sourceString) {
//        return SgTcpUtil.bytes2HexString(SgTcpUtil.IntToByteArray(Integer.parseInt(sourceString)));
        return SgTcpUtil.intStringToHexString(sourceString, 4);
    }


    public static void main(String[] args){
        TcpIncreaseCreditRequest tcpIncreaseCreditRequest = new TcpIncreaseCreditRequest();
        tcpIncreaseCreditRequest.setDeviceId("10001");
        tcpIncreaseCreditRequest.setIncrease("25");
        tcpIncreaseCreditRequest.setCoinAmount("22");
        tcpIncreaseCreditRequest.setAmount("11");
        System.out.println(tcpIncreaseCreditRequest.toString());

        String testString = SgTcpUtil.intStringToHexString("11", 6);
        System.out.println("testString = " + testString);

        System.out.println("=======08 ^ 01  ^ 03  ^ 00  ^ 16  ^ 00  ^ 0b  ^ 00 = 0X17========");

        //08 ^ 01  ^ 03  ^ 00  ^ 16  ^ 00  ^ 0b  ^ 00 = 0X17
        Integer n = Integer.parseInt("08",16)^
                Integer.parseInt("01",16)^
                Integer.parseInt("03",16)^
                Integer.parseInt("00",16)^
                Integer.parseInt("00",16)^
                Integer.parseInt("16",16)^
                Integer.parseInt("0b",16)^
                Integer.parseInt("00",16);
        System.out.println("n = " + n);
        String hexString = Integer.toHexString(n);
        System.out.println("check code:hexString = " + hexString);
    }

}

